package com.bigwolf.pe.controller;

import com.bigwolf.pe.constant.Constants;
import com.bigwolf.pe.domain.entity.SysMenu;
import com.bigwolf.pe.domain.entity.SysRole;
import com.bigwolf.pe.domain.entity.SysUser;
import com.bigwolf.pe.domain.model.LoginBody;
import com.bigwolf.pe.service.ISysMenuService;
import com.bigwolf.pe.service.SysLoginService;
import com.bigwolf.pe.service.SysPermissionService;
import com.bigwolf.pe.utils.AjaxResult;
import com.bigwolf.pe.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
//@RequestMapping("/process/main")
public class LoginController {

//    @Autowired
//    private SysRoleService sysRoleService;

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @PostMapping("/login")
    public AjaxResult list(@RequestBody  LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        System.out.println(loginBody.getUsername()+"=="+loginBody.getPassword()+"=="+loginBody.getCode()+"=="+loginBody.getUuid());
/*        List<SysRole> list =sysRoleService.selectRoleList(null);
        for (SysRole sysRole : list) {
            System.out.println(sysRole);
        }*/
        String token = loginService.login(loginBody.getUsername(),loginBody.getPassword(),loginBody.getCode(),loginBody.getUuid());

        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        //Long userId = 1L;
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }
}
